package com.ruoyi.web.controller.shop.member;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.shop.member.domain.ShopMemberOnline;
import com.ruoyi.shop.service.IShopOnlineService;
import com.ruoyi.shop.domain.model.ShopLoginUser;

/**
 * 在线会员用户controller
 * 
 * |----------------------------------------------------------------------|
 * |favorshop [简单好用，让您喜爱的小商城]
 * |----------------------------------------------------------------------|
 * |copyright www.favorshop.cn All rights reserved.
 * |----------------------------------------------------------------------|
 * | @author xuxiang
 * |----------------------------------------------------------------------|
 * | @date 2023-05-29
 * |----------------------------------------------------------------------|
 */
@Controller
@RequestMapping("/shop/member/online")
public class ShopMemberOnlineController extends BaseController {

	@Autowired
	private RedisCache redisCache;
	@Autowired
	private IShopOnlineService onlineService;

	private String prefix = "shop/member/user";

	@GetMapping()
	public String online() {
		return prefix + "/online";
	}

	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(String ipaddr, String userName) {
		Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
		List<ShopMemberOnline> onlineList = new ArrayList<ShopMemberOnline>();
		for (String key : keys) {
			ShopLoginUser user = redisCache.getCacheObject(key);
			if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
				if (StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUsername())) {
					onlineList.add(onlineService.selectOnlineByInfo(ipaddr, userName, user));
				}
			} else if (StringUtils.isNotEmpty(ipaddr)) {
				if (StringUtils.equals(ipaddr, user.getIpaddr())) {
					onlineList.add(onlineService.selectOnlineByIpaddr(ipaddr, user));
				}
			} else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getMember())) {
				if (StringUtils.equals(userName, user.getUsername())) {
					onlineList.add(onlineService.selectOnlineByUserName(userName, user));
				}
			} else {
				onlineList.add(onlineService.loginUserToMemberOnline(user));
			}
		}
		Collections.reverse(onlineList);
		onlineList.removeAll(Collections.singleton(null));
		return getDataTable(onlineList);
	}

	/**
	 * 强退用户
	 */
	@Log(title = "在线用户", businessType = BusinessType.FORCE)
	@PostMapping("/batchForceLogout")
	@ResponseBody
	public AjaxResult forceLogout(String ids) {
		for (String tokenId : Convert.toStrArray(ids)) {
			redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
		}
		return success();
	}
}
